var generateMatrix = function(n) {
    let ans =new Array(n).fill().map(()=>new Array(n).fill(0))
    ans[0][0] = 1;
    var i = 0;
    var j = 0;
    var cur = 1;
    while(cur<n*n) {
        while(j<n-1 && ans[i][j+1] == 0) {
            ans[i][++j] = ++cur;
            console.log(i,j,1)
        }
        while(i<n-1 && ans[i+1][j] == 0) {
            ans[++i][j] = ++cur;
            console.log(i,j,2)
        }
        while(j>0 && ans[i][j-1] == 0) {
            ans[i][--j] = ++cur;
            console.log(i,j,3)
        }
        while(i>0 && ans[i-1][j] == 0) {
            ans[--i][j] = ++cur;
            console.log(i,j,4)
        }
    }
    return ans;
    };
console.log(generateMatrix(3))